package com.czk.utlls;

import com.alibaba.fastjson.JSONObject;
import com.czk.pojo.tag.AddUserToTag;
import com.czk.pojo.tag.Tag;
import com.czk.pojo.tag.TagName;
import com.czk.pojo.tag.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/24 14:09
 */
@Component
public class UserTagUtil {
    @Autowired
    private TokenUtil tokenUtil;
    private String createTagUrl = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN";

    /**
     * 创建标签
     *
     * @param tagName
     */
    public void createTag(String tagName) {
        createTagUrl = createTagUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        // 构建请求
        TagName name = new TagName();
        name.setName(tagName);
        Tag tag = new Tag();
        tag.setTag(name);
        // 转json
        String jsonString = JSONObject.toJSONString(tag);
        String str = WeChatUtil.post(createTagUrl, jsonString);
        System.out.println(str);
    }

    private String getTagUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN";

    /**
     * 获取标签
     *
     * @return
     */
    public String getTags() {
        getTagUrl = getTagUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        String result = HttpUtil.get(getTagUrl);
        return result;
    }

    private String updateTagUrl = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=ACCESS_TOKEN";

    /**
     * 更新tag
     *
     * @param tagId
     * @param tagName
     */
    public void updateTag(Integer tagId, String tagName) {
        updateTagUrl = updateTagUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        // 构建json
        String json = "{   \"tag\" : {     \"id : " + tagId + "\"name\" :" + tagName + " } } ";
        String result = WeChatUtil.post(updateTagUrl, json);
        System.out.println(result);
    }

    private String addUserToTagUrl = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN";

    /**
     * 为用户打包标签
     *
     * @param tagid
     * @param userIds
     */
    public void addUserToTag(Integer tagid, List<String> userIds) {
        AddUserToTag addUserToTag = new AddUserToTag();
        addUserToTag.setTagid(tagid);
        addUserToTag.setOpenid_list(userIds);
        addUserToTagUrl = addUserToTagUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        System.out.println(WeChatUtil.post(addUserToTagUrl, JSONObject.toJSONString(addUserToTag)));
    }

    private String getUserByTagUrl = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=ACCESS_TOKEN";

    /**
     * 拿到tag下的用户
     *
     * @param tagId
     */
    public void getUserByTag(Integer tagId) {
        // 地址
        getUserByTagUrl = getUserByTagUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        String json = "{   \"tagid\" : " + tagId + "}";
        System.out.println(WeChatUtil.post(getUserByTagUrl, json));
    }

    private String getTagByUserUrl = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=ACCESS_TOKEN";

    /**
     * 拿到user对应的tag
     *
     * @param userId
     */
    public void getTagByUser(String userId) {
        User user=new User();
        user.setOpenid(userId);
        getTagByUserUrl = getTagByUserUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        System.out.println(WeChatUtil.post(getTagByUserUrl, JSONObject.toJSONString(user)));
    }
}
